package com.myframework.smhj.hibernate;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**
 * 分页信息
 *
 * @author mo_yq5
 * @since 2016年4月10日
 */
public class Searching implements Paging, Sorting {

	/**
	 * 最大分页大小
	 */
	public static final int MAX_PAGE_SIZE = 100;

	/**
	 * 是否查询总记录数
	 */
	@JsonIgnore
	private boolean isCount = true;

	/**
	 * 显示链接数
	 */
	@JsonSerialize
	private int linkCount = 7;

	/**
	 * 当前页码
	 */
	@JsonSerialize
	private int nowPage = 1;

	/**
	 * 分页大小
	 */
	@JsonSerialize
	private int pageSize = 20;

	/**
	 * 查询关键字
	 */
	@JsonSerialize
	private String searchWord;

	/**
	 * 排序方式
	 */
	private List<Sort> sorts;
	/**
	 * 总页数
	 */
	@JsonSerialize
	private int totalPage = 1;

	/**
	 * 总记录数
	 */
	@JsonSerialize
	private int totalResult = 0;

	@Override
	public int getFirstResult() {
		return (getNowPage() - 1) * getPageSize();
	}

	@Override
	public boolean getIsCount() {
		return isCount;
	}

	@Override
	public int getLinkCount() {
		return linkCount;
	}

	@Override
	public int getNowPage() {
		if (nowPage < 1) {
			nowPage = 1;
		} else if (nowPage > getTotalPage()) {
			nowPage = getTotalPage() == 0 ? 1 : getTotalPage();
		}
		return nowPage;
	}

	@Override
	public int getPageSize() {
		if (pageSize <= 0) {
			pageSize = 1;
		} else if (pageSize > MAX_PAGE_SIZE) {
			pageSize = MAX_PAGE_SIZE;
		}
		return pageSize;
	}

	public String getSearchWord() {
		return searchWord;
	}

	@Override
	public List<Sort> getSorts() {
		return sorts;
	}

	@Override
	public int getTotalPage() {
		totalPage = (getTotalResult() / getPageSize() + (getTotalResult()
				% getPageSize() > 0 ? 1 : 0));
		return totalPage;
	}

	@Override
	public int getTotalResult() {
		if (totalResult < 0) {
			totalResult = 0;
		}
		return totalResult;
	}

	@Override
	public void setIc(boolean ic) {
		setIsCount(ic);
	}

	@Override
	public void setIsCount(boolean isCount) {
		this.isCount = isCount;
	}

	@Override
	public void setLc(int lc) {
		setLinkCount(lc);
	}

	@Override
	public void setLinkCount(int linkCount) {
		this.linkCount = linkCount;
	}

	@Override
	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}

	@Override
	public void setNp(int np) {
		setNowPage(np);
	}

	@Override
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Override
	public void setPs(int ps) {
		setPageSize(ps);
	}

	public void setSearchWord(String searchWord) {
		this.searchWord = searchWord;
	}

	@Override
	public void setSorts(List<Sort> sorts) {
		this.sorts = sorts;
	}

	@Override
	public void setTotalResult(int totalResult) {
		this.totalResult = totalResult;
	}

}
